package com.house.oss.upload;

import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.common.comm.Protocol;
import com.aliyun.oss.common.comm.SignVersion;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author
 * @version 1.0
 * @description: TODO
 * @date 2025/9/17 15:24
 */
@Configuration
public class AliOssConfig {

    @Value("${oss.accessKeyId}")
    private String ossAccessKeyId;
    @Value("${oss.accessKeySecret}")
    private String ossAccessKeySecret;
    @Bean
    public OSS ossClient() {
        // 创建 ClientBuilderConfiguration 实例，用于配置 OSS 客户端参数
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        // 设置签名算法版本为 V4
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        // 设置使用 HTTPS 协议访问 OSS，保证传输安全性
        clientBuilderConfiguration.setProtocol(Protocol.HTTPS);

        // 创建 OSS 客户端实例
        return  OSSClientBuilder.create()
                // 以华东1（杭州）地域的外网访问域名为例，Endpoint填写为oss-cn-hangzhou.aliyuncs.com
                .endpoint("oss-cn-beijing.aliyuncs.com")
                // 从环境变量中获取访问凭证（需提前配置 OSS_ACCESS_KEY_ID 和 OSS_ACCESS_KEY_SECRET）
//                .credentialsProvider(CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider())
                .credentialsProvider(new DefaultCredentialProvider(ossAccessKeyId, ossAccessKeySecret))
                // 设置客户端配置
                .clientConfiguration(clientBuilderConfiguration)
                // 以华东1（杭州）地域为例，Region填写为cn-hangzhou
                .region("cn-beijing")
                .build();
//        try {
//            // 列出当前用户的所有 Bucket
//            List<Bucket> buckets = ossClient.listBuckets();
//            // 遍历打印每个 Bucket 的名称
//            for (Bucket bucket : buckets) {
//                System.out.println(bucket.getName());
//            }
//        } finally {
//            // 当OSSClient实例不再使用时，调用shutdown方法以释放资源
//            ossClient.shutdown();
//        }
    }
}
